﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;
using MindMelder.Core;
using MindMelder.Core.DataInterfaces;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;

namespace MindMelder.Web.Controllers
{
	[HandleError]
    public class MasterController : Controller
	{
		public MasterController(IUserRepository userRepository)
		{
			Check.Require(userRepository != null, "userRepository may not be null");
			this.userRepository = userRepository;
		}

		public ActionResult LoggedInUser()
		{
			ViewData["LoggedIn"] = User.Identity.IsAuthenticated;
			if (User.Identity.IsAuthenticated)
			{
				User user = userRepository.GetByOpenId(User.Identity.Name);
				if (user != null)
				{
					ViewData["DisplayName"] = user.FullName;
				}
			}
			return View();
		}

		public ActionResult Messages()
		{
			if (User.Identity.IsAuthenticated)
			{
				User user = userRepository.GetByOpenId(User.Identity.Name);
				if (user != null && user.Messages.Where(m => !m.IsRead).Count() > 0)
				{
					ViewData["notifications"] = user.Messages.Count;
				}
			}
			return View();
		}

		private readonly IUserRepository userRepository;
	}
}
